package edu.washington.ling.syntric.io;

import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.washington.ling.syntric.util.XMLUtils;

public class ExampleDocReaderXML extends ExampleDocReader {
	
	protected ExampleDocReaderXML(String path) {
		super(path);
	}
	public ExampleDoc read() throws IOException {
		ExampleDoc exDoc = new ExampleDoc();
		Document doc = XMLUtils.readXMLFile(this.getPath());
		NodeList sentNodeList = doc.getElementsByTagName("Sentence");
		for (int i = 0; i < sentNodeList.getLength(); i++) {
			Element sentNode = (Element) sentNodeList.item(i);
			NamedNodeMap sentNodeAttrs = sentNode.getAttributes();
			Node idNode = sentNodeAttrs.getNamedItem("id");
			String id = idNode.getNodeValue();
			Example ex = new Example(id);
			NodeList origNodeList = sentNode.getElementsByTagName("Original");
			if (origNodeList.getLength() > 0) {
				Element origNode = (Element) origNodeList.item(0);
				ex.setOriginal(origNode.getTextContent());
			}
			NodeList glossNodeList = sentNode.getElementsByTagName("Gloss");
			if (glossNodeList.getLength() > 0) {
				Element glossNode = (Element) glossNodeList.item(0);
				ex.setGloss(glossNode.getTextContent());
			}
			NodeList transNodeList = sentNode.getElementsByTagName("Translation");
			if (transNodeList.getLength() > 0) {
				Element transNode = (Element) transNodeList.item(0);
				ex.setTranslation(transNode.getTextContent());
			}
			NodeList treeNodeList = sentNode.getElementsByTagName("Tree");
			for (int j = 0; j < treeNodeList.getLength(); j++) {
				Element treeNode = (Element) treeNodeList.item(j);
				NamedNodeMap treeNodeAttrs = treeNode.getAttributes();
				Node layerNode = treeNodeAttrs.getNamedItem("layer");
				String layer = "none";
				if (layerNode != null) {
					layer = layerNode.getNodeValue();
				} 
				Node formatNode = treeNodeAttrs.getNamedItem("format");
				String format = "none";
				if (formatNode != null) {
					format = formatNode.getNodeValue();
				}
				String[] treeLines = treeNode.getTextContent().split("\\r?\\n");
				TreeRepresentation tr = new TreeRepresentation(treeLines, layer, format);
				Node metaDataNode = treeNodeAttrs.getNamedItem("metadata");
				if (metaDataNode != null) {
					String metaDataStr = metaDataNode.getNodeValue();
					if (metaDataStr.length() > 0) {
						tr.setMetaData(metaDataNode.getNodeValue().split(";"));
					}
				}
				ex.addTreeRep(tr);
			}
			NodeList sysTreeNodeList = sentNode.getElementsByTagName("SystemTree");
			for (int j = 0; j < sysTreeNodeList.getLength(); j++) {
				Element sysTreeNode = (Element) sysTreeNodeList.item(j);
				NamedNodeMap treeNodeAttrs = sysTreeNode.getAttributes();
				Node layerNode = treeNodeAttrs.getNamedItem("layer");
				String layer = "none";
				if (layerNode != null) {
					layer = layerNode.getNodeValue();
				} 
				Node formatNode = treeNodeAttrs.getNamedItem("format");
				String format = "none";
				if (formatNode != null) {
					format = formatNode.getNodeValue();
				}
				String[] treeLines = sysTreeNode.getTextContent().split("\\r?\\n");
				TreeRepresentation tr = new TreeRepresentation(treeLines, layer, format);
				Node metaDataNode = treeNodeAttrs.getNamedItem("metadata");
				if (metaDataNode != null) {
					String metaDataStr = metaDataNode.getNodeValue();
					if (metaDataStr.length() > 0) {
						tr.setMetaData(metaDataNode.getNodeValue().split(";"));
					}
				}
				ex.addSystemTreeRep(tr);
			}
			if (exDoc.hasExample(id)) {
				String errStr = String.format("Error: example '%s' has already been seen.\n", id);
				System.err.println(errStr);
			} else {
				exDoc.addExample(ex);
			} 
		}
		exDoc.resetIterator();
		return exDoc;
	}

}
